unit fPovMain;

interface

uses
  Winapi.Windows,
  System.Classes,
  System.UITypes,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,
  System.SysUtils,
  Vcl.Buttons,
  Vcl.Controls,
  Vcl.ComCtrls,
  Vcl.ExtCtrls;

type
  TMainForm = class(TForm)
    POVRayGroupBox: TGroupBox;
    Label1: TLabel;
    POVRayExecutableEdit: TEdit;
    BrowseSpeedButton1: TSpeedButton;
    Label2: TLabel;
    CommandLineEdit: TEdit;
    ImageSettingsGroupBox: TGroupBox;
    Label3: TLabel;
    WidthNumberEdit: TEdit;
    Label4: TLabel;
    HeightNumberEdit: TEdit;
    GroupBox1: TGroupBox;
    Label5: TLabel;
    POVRayOutputFileEdit: TEdit;
    BrowseSpeedButton2: TSpeedButton;
    CoordinateAxesCheckBox: TCheckBox;
    NormalizeTreeCheckBox: TCheckBox;
    Label6: TLabel;
    ImageOutputFileEdit: TEdit;
    BrowseSpeedButton3: TSpeedButton;
    Label7: TLabel;
    ImageFormatComboBox: TComboBox;
    ApplyButton: TButton;
    CancelButton: TButton;
    OKButton: TButton;
    OpenDialog: TOpenDialog;
    ImageTotalEdit: TEdit;
    Label8: TLabel;
    AnimationRG: TRadioGroup;
    RotationTrackBar: TTrackBar;
    RotaLabel: TLabel;
    ColorComboBox: TComboBox;
    Label9: TLabel;
    Label10: TLabel;
    procedure ApplyButtonClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure BrowseSpeedButton1Click(Sender: TObject);
    procedure BrowseSpeedButton2Click(Sender: TObject);
    procedure BrowseSpeedButton3Click(Sender: TObject);
    procedure RotationTrackBarChange(Sender: TObject);
  private

    POVPath, PovIniPath, ImagePath: String;
  public

  end;

//--------------------------------------------------------------------------
implementation

{$R *.dfm}

procedure TMainForm.ApplyButtonClick(Sender: TObject);
var
  InString: String;
  f: TextFile;
begin
  { ApplyButton.Enabled := False; }
  try
    AssignFile(f, ExtractFilePath(ParamStr(0)) + 'PovTreal.ini');
    try
      Rewrite(f);
      If CoordinateAxesCheckBox.Checked then
        InString := 'T'
      else
        InString := 'F';
      Writeln(f, InString); { IncludeAxes }
      If NormalizeTreeCheckBox.Checked then
        InString := 'T'
      else
        InString := 'F';
      Writeln(f, InString); { NormalizeTreeDimensions }
      InString := POVRayExecutableEdit.Text;
      Writeln(f, InString); // POVRayExecutable
      InString := CommandLineEdit.Text;
      Writeln(f, InString); // POVRayCommandLine
      InString := POVRayOutputFileEdit.Text;
      Writeln(f, InString); // POVRayInputFileName
      InString := ImageOutputFileEdit.Text;
      Writeln(f, InString); // ImageFileName
      InString := Inttostr(ImageFormatComboBox.ItemIndex);
      Writeln(f, InString); // ImageFormat
      { ImageWidth := } InString := WidthNumberEdit.Text;
      Writeln(f, InString);
      { ImageHeight := } InString := HeightNumberEdit.Text;
      Writeln(f, InString);
      { ImageBColor } InString := Inttostr(ColorComboBox.ItemIndex);
      Writeln(f, InString);
      { Rotation } InString := Inttostr(RotationTrackBar.Position);
      Writeln(f, InString);
      { Animation } InString := Inttostr(AnimationRG.ItemIndex);
      Writeln(f, InString);
      { ImageTotal := StrtoInt(InString); } InString := ImageTotalEdit.Text;
      Writeln(f, InString);
      Writeln(f, POVPath);
      Writeln(f, PovIniPath);
      Writeln(f, ImagePath);
    except
      on EInOutError do
      begin
        MessageDlg('PovTreal.ini corrupt!', mtError, [mbOK], 0);
      end;
    end;
  finally
    CloseFile(f);
  end;
end;

procedure TMainForm.FormActivate(Sender: TObject);
var
  InString: String;
  f: TextFile;
begin
  POVPath := ExtractFilePath(ParamStr(0));
  PovIniPath := ExtractFilePath(ParamStr(0));
  ImagePath := ExtractFilePath(ParamStr(0));
  { ApplyButton.Enabled := False; }
  ImageFormatComboBox.ItemIndex := 0; { 2 i like png, but it is bad? }
  ColorComboBox.ItemIndex := 0;
  WidthNumberEdit.Text := Inttostr(800);
  HeightNumberEdit.Text := Inttostr(600);
  ImageTotalEdit.Text := Inttostr(1);
  if FileExists(ExtractFilePath(ParamStr(0)) + 'PovTreal.ini') then
  begin { Read the settings from the Treal.ini }
    try
      AssignFile(f, ExtractFilePath(ParamStr(0)) + 'PovTreal.ini');
      try
        Reset(f);
        Readln(f, InString);
        If InString = 'T' then { IncludeAxes }
          CoordinateAxesCheckBox.Checked := True
        else { IncludeAxes }
          CoordinateAxesCheckBox.Checked := False;
        Readln(f, InString);
        If InString = 'T' then { NormalizeTreeDimensions }
          NormalizeTreeCheckBox.Checked := True
        else { NormalizeTreeDimensions }
          NormalizeTreeCheckBox.Checked := False;
        Readln(f, InString); { POVRayExecutable }
        POVRayExecutableEdit.Text := InString;
        Readln(f, InString); { POVRayCommandLine }
        CommandLineEdit.Text := InString;
        Readln(f, InString); { POVRayInputFileName }
        POVRayOutputFileEdit.Text := InString;
        Readln(f, InString); { ImageFileName }
        ImageOutputFileEdit.Text := InString;
        Readln(f, InString); { ImageFormat }
        ImageFormatComboBox.ItemIndex := StrtoInt(InString);
        Readln(f, InString); { ImageWidth := }
        WidthNumberEdit.Text := (InString);
        Readln(f, InString); { ImageHeight := }
        HeightNumberEdit.Text := (InString);
        Readln(f, InString); { ImageBColor }
        ColorComboBox.ItemIndex := StrtoInt(InString);
        Readln(f, InString); { Rotation }
        RotationTrackBar.Position := StrtoInt(InString);
        Readln(f, InString); { Animation }
        AnimationRG.ItemIndex := StrtoInt(InString);
        Readln(f, InString);
        ImageTotalEdit.Text := InString;
        Readln(f, POVPath);
        Readln(f, PovIniPath);
        Readln(f, ImagePath);
      except
        on EInOutError do
        begin
          MessageDlg('PovTreal.ini corrupt!', mtError, [mbOK], 0);
        end;
      end;
    finally
      CloseFile(f);
    end;
  end
  else
  begin
    try
      AssignFile(f, ExtractFilePath(ParamStr(0)) + 'PovTreal.ini');
      try
        Rewrite(f);
        If CoordinateAxesCheckBox.Checked then
          InString := 'T'
        else
          InString := 'F';
        Writeln(f, InString); { IncludeAxes }
        If NormalizeTreeCheckBox.Checked then
          InString := 'T'
        else
          InString := 'F';
        Writeln(f, InString); { NormalizeTreeDimensions }
        { POVRayExecutable } InString := POVRayExecutableEdit.Text;
        Writeln(f, InString);
        { POVRayCommandLine } InString := CommandLineEdit.Text;
        Writeln(f, InString);
        { POVRayInputFileName } InString := POVRayOutputFileEdit.Text;
        Writeln(f, InString);
        { ImageFileName }
        InString := ChangeFileExt(ImageOutputFileEdit.Text, '.tga');
        Writeln(f, InString);
        { ImageFormat }
        InString := Inttostr(0 { ImageFormatComboBox.ItemIndex } );
        Writeln(f, InString);
        { ImageWidth := } InString := WidthNumberEdit.Text;
        Writeln(f, InString);
        { ImageHeight := } InString := HeightNumberEdit.Text;
        Writeln(f, InString);
        { ImageBColor } InString := Inttostr(ColorComboBox.ItemIndex);
        Writeln(f, InString);
        { Rotation } InString := Inttostr(RotationTrackBar.Position);
        Writeln(f, InString);
        { Animation } InString := Inttostr(AnimationRG.ItemIndex);
        Writeln(f, InString);
        { ImageTotal := StrtoInt(InString); } InString := ImageTotalEdit.Text;
        Writeln(f, InString);
        Writeln(f, POVPath);
        Writeln(f, PovIniPath);
        Writeln(f, ImagePath);
      except
        on EInOutError do
        begin
          MessageDlg('PovTreal.ini corrupted!', mtError, [mbOK], 0);
        end;
      end;
    finally
      CloseFile(f);
    end;
  end;
end;

procedure TMainForm.OKButtonClick(Sender: TObject);
begin
  if ApplyButton.Enabled then
    ApplyButtonClick(Sender);
  Self.Close;
end;

procedure TMainForm.CancelButtonClick(Sender: TObject);
begin
  Self.Close;
end;

procedure TMainForm.BrowseSpeedButton1Click(Sender: TObject);
begin
  OpenDialog.InitialDir := POVPath;
  OpenDialog.Title := 'Select POV-Ray executable...';
  OpenDialog.Filter := 'Executable files (*.exe)|*.EXE|All files|*.*';
  OpenDialog.DefaultExt := '*.EXE';
  if OpenDialog.Execute then
  begin
    POVRayExecutableEdit.Text := OpenDialog.FileName;
    POVPath := ExtractFilePath(OpenDialog.FileName);
  end;
end;

procedure TMainForm.BrowseSpeedButton2Click(Sender: TObject);
begin
  OpenDialog.InitialDir := PovIniPath;
  OpenDialog.Title := 'Select POV-Ray output file...';
  OpenDialog.Filter := 'POV-Ray files (*.pov)|*.POV|All files|*.*';
  OpenDialog.DefaultExt := '*.POV';
  if OpenDialog.Execute then
  begin
    POVRayOutputFileEdit.Text := OpenDialog.FileName;
    PovIniPath := ExtractFilePath(OpenDialog.FileName);
  end;
end;

procedure TMainForm.BrowseSpeedButton3Click(Sender: TObject);
begin
  OpenDialog.InitialDir := ImagePath;
  OpenDialog.Title := 'Select image output file...';
  OpenDialog.Filter := 'Targa files (*.tga)|*.TGA';
  { 'Targa files (*.tga)|*.TGA|PNG files (*.PNG)|Unix PPM files (*.ppm)|*.PPM|All files|*.*'; }
  OpenDialog.DefaultExt := '*.tga';
  if OpenDialog.Execute then
  begin
    ImageOutputFileEdit.Text := OpenDialog.FileName;
    ImagePath := ExtractFilePath(OpenDialog.FileName);
  end;
end;

procedure TMainForm.RotationTrackBarChange(Sender: TObject);
begin
  RotaLabel.Caption := Inttostr(RotationTrackBar.Position);
end;

end.
